OUTPUT_DIR = ./bin/
MAIN_SOURCES = ./src/main/*.java ./src/main/remote/*.java
MAIN_CLASSES = ./bin/main/*.class ./bin/main/remote/*.class
TEST_SOURCES = ./src/test/*.java ./src/test/remote/*.java
TEST_CLASSES = ./bin/test/*.class ./bin/remote/*.class
TEST_CLASSPATH = lib/junit/junit.jar:lib/junit/hamcrest.jar:./bin/
TEST_CASES = GraphTest TaskTest ParserTest TaskDispatcherTest CommandTest remote.ClientTest remote.MessageTest remote.RemoteExecutorTest remote.ServerDispatcherTest remote.TaskServiceTest
MAIN_JAR = ./bin/jar/hw3.jar
SERVER_JAR = ./bin/jar/server.jar

$(MAIN_JAR): $(MAIN_CLASSES)
# Create client jar
	jar -cfe $(MAIN_JAR) main.Main -C ./bin ./main
# Create server jar
	jar -cfe $(SERVER_JAR) main.remote.TaskService -C ./bin ./main

$(MAIN_CLASSES): $(MAIN_SOURCES)
	javac -d $(OUTPUT_DIR) $(MAIN_SOURCES)

$(TEST_CLASSES): $(MAIN_CLASSES) $(TEST_SOURCES)
	javac -cp $(TEST_CLASSPATH) -d $(OUTPUT_DIR) $(TEST_SOURCES)

run_server: $(SERVER_JAR)
# Run Server in background
	java -cp ./bin -jar $(SERVER_JAR) &
run_client: $(MAIN_JAR)
# Run Client task executor
	java -cp ./bin -jar $(MAIN_JAR)

check: $(TEST_CLASSES)
	echo "Running tests..."
	for test in $(TEST_CASES); do \
		echo "Running test $$test"; \
		java -cp ./bin:./lib/junit/hamcrest.jar:./lib/junit/junit.jar org.junit.runner.JUnitCore test.$$test; \
	done

clean:
	rm -rf bin
